package com.noble.glamour.ui.agora.act.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.noble.glamour.R;
import com.noble.glamour.bean.my.GiftsList;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.view.base.BaseDialogFragment;
import com.noble.glamour.module.logic.invoke.Invoker;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;

import java.util.ArrayList;
import java.util.List;

/**
 * 视频中送礼加好友
 * <p>
 * Created by Su on 2017/7/8.
 */
public class RtcVideoGiftDialog extends BaseDialogFragment implements View.OnClickListener, RtcVideoGiftPanel.GiftPanelItemClickListener, RequestComplete {
    private int position = 0;
    private List<GiftsList.GiftInfo> list;

    public RtcVideoGiftDialog() {
        settWindowAnimations(R.style.AnimDownInDownOutOverShoot);
        setGravity(Gravity.BOTTOM);
        setDialogSizeRatio(0, 0);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.f1_chat_video_gift_dialog);
        initView();
        return getContentView();
    }

    private void initView() {
        LinearLayout container = (LinearLayout) findViewById(R.id.gift_list);
        RtcVideoGiftPanel giftPanel = new RtcVideoGiftPanel(getContext(), initGiftList());
        giftPanel.setPanelItemClickListener(this);
        container.addView(giftPanel.getContentView());

        findViewById(R.id.gift_send).setOnClickListener(this);
    }

    private List<GiftsList.GiftInfo> initGiftList() {
        list = new ArrayList<>();
        List<Integer> ids = ModuleMgr.getCommonMgr().getCommonConfig().getGift().getNormal_friends_add_ids();
        GiftsList giftsList = ModuleMgr.getCommonMgr().getGiftLists();

        if (ids == null || giftsList == null) return list;
        for (int i = 0; i < ids.size(); i++) {
            list.add(giftsList.getGiftInfo(ids.get(i)));
        }
        return list;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gift_send:        // 发送礼物
                sendGift();
                break;
        }
    }

    private void sendGift() {
        try {
            if (list.get(position).getMoney() > ModuleMgr.getCenterMgr().getMyInfo().getDiamand()) {
                PToast.showShort(getContext().getString(R.string.chat_man_diamond_insufficient));
                return;
            }

            // 通知H5
            Invoker.getInstance().giftMessageJSCMD(list.get(position).getPic(), list.get(position).getId(), 1, 1);

            // 发送礼物
            long otherId = ModuleMgr.getRtcEnginMgr().getEngineConfig().mOtherId;
            ModuleMgr.getChatMgr().sendGiftMsg(null, String.valueOf(otherId), list.get(position).getId(), 1, 5, this); // TODO
        } catch (Exception e) {
            PToast.showShort(getContext().getString(R.string.chat_man_send_gift_fail));
        }
    }

    @Override
    public void OnPanelItemClick(View convertView, int position) {
        this.position = position;
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.isOk()) {
            PToast.showShort(getContext().getString(R.string.chat_man_send_gift_suc));
            MsgMgr.getInstance().sendMsg(MsgType.MT_Rtc_Chat_Send_Gift, null);
            dismiss();
            return;
        }

        PToast.showShort(getContext().getString(R.string.chat_man_send_gift_fail));
    }
}
